# Edit this configuration file to define what should be installed on
# your system.  Help is available in the configuration.nix(5) man page
# and in the NixOS manual (accessible by running ‘nixos-help’).
{
  config,
  lib,
  pkgs,
  ...
}: {
  imports = [];
  # Bootloader
  boot.loader = {
    systemd-boot.enable = true;
    efi.canTouchEfiVariables = true;
  };
  networking = {
    hostName = "nixos"; # hostname
    networkmanager.enable = true;
    # conflict with networkmanager
    # wireless.enable = true; # Enables wireless support via wpa_supplicant.
    proxy = {
      default = "";
      noProxy = "127.0.0.1,localhost,internal.domain";
    };
  };
  i18n = {
    defaultLocale = "en_US.UTF-8";
    supportedLocales = [
      "en_US.UTF-8/UTF-8"
      "zh_CN.UTF-8/UTF-8"
    ];
    extraLocaleSettings = {
      LC_ADDRESS = "zh_CN.UTF-8";
      LC_IDENTIFICATION = "zh_CN.UTF-8";
      LC_MEASUREMENT = "zh_CN.UTF-8";
      LC_MONETARY = "zh_CN.UTF-8";
      LC_NAME = "zh_CN.UTF-8";
      LC_NUMBER = "zh_CN.UTF-8";
      LC_PAPER = "zh_CN.UTF-8";
      LC_TELEPHONE = "zh_CN.UTF-8";
      LC_TIME = "zh_CN.UTF-8";
    };
    inputMethod = {
      type = "ibus";
      enable = true;
      ibus.engines = with pkgs.ibus-engines; [
        uniemoji
        libpinyin
        rime
        table
        table-chinese
        typing-booster
      ];
    };
  };
  services = {
    libinput.enable = false; # will cause surfacego freeze! no response with touch screen gesture, maybe conflict with hardware-surfacego???
    xserver = {
      enable = true;
      displayManager = {
        gdm.enable = true;
        # Enable automatic login for the user
        autoLogin = {
          enable = true;
          user = "humphrey";
        };
      };
      desktopManager.gnome.enable = true;
      # Enable touchpad support(enabled default in most desktopManager)
      # libinput.enable = true; deprecated!
      # Configure keymap in X11
      xkb = {
        layout = "cn";
        variant = "";
      };
      excludePackages = [pkgs.xterm];
    };
    # Enable CUPS to print documents
    printing.enable = true;
    pipewire = {
      enable = true;
      #alsa.enable = false;
      #alsa.support32Bit = false;
      # If you want to use JACK applications, uncomment this
      # jack.enable = true;
      # use the example session manager (no others are packages yet so this is
      # enabled by default, no need to redefine it in your config for now)
      # media-session.enable = true;
    };
    # List services that you want to enable;

    # Enable the OpenSSH daemon;
    openssh.enable = true;
  };
  fonts = {
    packages = with pkgs; [
      # nerdfonts
      recursive
      material-design-icons
      font-awesome
      powerline-fonts
      # https://github.com/NixOS/nixpkgs/blob/nixos-24.05/pkgs/data/fonts/nerdfonts/shas.nix
      (nerdfonts.override {
        fonts = [
          "Meslo"
          # symbols icon only
          "NerdFontsSymbolsOnly"
          # Characters
          #"FiraCode"
          "JetBrainsMono"
          #"Iosevka"
        ];
      })
    ];
    fontconfig = {
      useEmbeddedBitmaps = true;
    };
  };
  # Enable sound with pipewire
  hardware.pulseaudio.enable = false; # will conflict with pipewire.
  security.rtkit.enable = true;

  # Workaround for GNOME autologin: https://github.com/NixOS/nixpkgs/issues/103746#issuecomment-945091229
  systemd.services = {
    "getty@tty1".enable = false;
    "autovt@tty1".enable = false;
  };

  # Install firefox
  # programs.firefox.enable = true;

  # nix settings
  nix = {
    settings = {
      # Optionally disable local building
      max-jobs = "auto";
      experimental-features = ["nix-command" "flakes"];
      substituters = ["https://mirror.ustc.edu.cn/nix-channels/store"];
    };
    # distributedBuilds
    distributedBuilds = true;
    extraOptions = ''
      builders-use-substitutes = true
    '';
    buildMachines = [
      {
        hostName = "1.14.67.92";
        system = pkgs.system;
        maxJobs = 10;
        sshUser = "root";
        publicHostKey = "/home/humphrey/.ssh/nixdist.pub";
        sshKey = "/home/humphrey/.ssh/nixdist";
        supportedFeatures = ["nixos-test" "benchmark" "big-parallel" "kvm"];
        mandatoryFeatures = ["big-parallel"];
      }
    ];
  };
  # nix config
  nixpkgs.config = {
    # Allow unfree packages
    allowUnfree = true;
    allowUnsupportedSystem = true;
    allowBroken = true;
  };
  time.timeZone = "Asia/Shanghai";

  # Define a use account, Don't forget to set a password with `passwd`
  users = {
    mutableUsers = true; # 禁止useradd添加用户 and will cause lock out of system
    users = {
      humphrey = {
        isNormalUser = true;
        description = "humphrey";
        extraGroups = [
          "wheel"
          "users"
          "docker"
          "media"
          "surface-control"
        ];
        packages = with pkgs; [];
      };
    };
  };
  # List packages installed in system profile. To search run :
  # $ nix search wget;
  environment = {
    systemPackages = with pkgs;
      [
        # vim # Do not forget to add an editor to edit configuration.nix! The Nano editor is also installed by default;
        wget
        git
        neovim
        # obsidian
      ]
      ++ lib.optionals (stdenv.isLinux && stdenv.isx86_64) [
        wineWowPackages.full
        wineWow64Packages.full
      ];
    variables = {
      EDITOR = "nvim";
    };
    gnome.excludePackages = with pkgs.gnome; [
      baobab # disk usafe analyzer
      cheese # photo booth
      # eog # image viewer
      # epiphany # web browser
      # gedit # text editor
      simple-scan # document scanner
      # totem # video player
      yelp # help viewer
      # evince # document viewer
      # file-roller # archive manager
      geary # email client
      seahorse # password manager
      # these should be self explanatory
      # gnome-calculator
      # gnome-calendar
      # gnome-characters
      # gnome-clocks
      gnome-contacts
      # gnome-font-viewer
      # gnome-logs
      gnome-maps
      # gnome-music
      pkgs.gnome-photos
      # gnome-screenshot
      # gnome-system-monitor
      gnome-weather
      # gnome-disk-utility
      # gnome-connections
      pkgs.gnome-tour
    ];
  };
  # Some program need SUID wrappers, can be configured further or are
  # started in user sessions;
  programs = {
    mtr.enable = true;
    gnupg.agent = {
      enable = true;
      enableSSHSupport = true;
    };
  };
  system.stateVersion = "24.11";
  virtualisation.waydroid.enable = true;
  # enable surface-control, control various aspects of Surface devices on Linux from command line.
  # config.microsoft-surface.surface-control.enable = true;
}
